//
//  MBSpacePerfectInformationViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpacePerfectInformationViewController.h"
#import "MBSpaceInFormationViewController.h"
#import "MBTabBarController.h"
#import "AppDelegate.h"
#import "AFNetworking.h"

typedef enum {
    IdentityType_Mom = 10,
    IdentityType_Bad,
    IdentityType_Other,
    IdentityType_Box,
}IdentityType;

typedef enum {
    BabySexType_Boy = 30,
    BabySexType_Girl,
    BabySexType_Seciry,
}BabySexType;

@interface MBSpacePerfectInformationViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate> {
    UIScrollView                    *_scrollView;
    
    UIView                          *headView;
    UILabel                         *headLb;
    UIImageView                     *rightView;
    UIImageView                     *_headImgView;
    UIButton                        *headImgBt;
    
    UILabel                         *identityLb;
    UIView                          *identityView;
    
    UIView                          *_nickView;
    UILabel                         *nickLb;
    UILabel                         *_nickNameLb;
    UIImageView                     *nickRightView;
    UIButton                        *nickBt;
    
    UIView                          *_babyView;
    UIView                          *babyNameView;
    UILabel                         *babyNmLb;
    UILabel                         *_babyNameLb;
    UIImageView                     *babyRightView;
    UIButton                        *babyNameBt;
    UIView                          *line2View;
    UILabel                         *babySRLb;
    UILabel                         *_babyBirthDayLb;
    UIImageView                     *babyRight2View;
    UIButton                        *babyBirthDayBt;
    
    UILabel                         *babySexLb;
    UIView                          *babySexView;
    
    UIDatePicker                    *_myDatePicker;
    UIView                          *_datePickerView;
    UIButton                        *cancelBt;
    UIButton                        *sureBt;
    UIView                          *_showderView;
    UIPopoverController             *m_pPopoverController;
}

@property (nonatomic,assign)IdentityType    identityType;
@property (nonatomic,assign)BabySexType     babySexType;

@end

@implementation MBSpacePerfectInformationViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self onUpdateUIForView:self.orientation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleView:@"完善资料"];
    _identityType = IdentityType_Mom;
    
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.frame = CGRectMake(0, 0, 60, 30);
    [bt setTitle:@"完成" forState:UIControlStateNormal];
    [bt setTitle:@"完成" forState:UIControlStateHighlighted];
    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    bt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [bt addTarget:self action:@selector(onSelectItemForWork) forControlEvents:UIControlEventTouchUpInside];
    [self setRightBarItems:bt];
    
    [self setBackItem];
    [self onCreateUI];
    [self onLoadData];
    
}

- (void)back
{
    if (_isFromRegister) {
        MBTabBarController *tabBar = [[MBTabBarController alloc] init];
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        delegate.window.rootViewController = tabBar;
    } else {
        [super back];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//MARK:UI
- (void)onCreateUI
{
    UserModel *model = [UserManager standerDefult].userModel;
    
    //背景图
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    //头像
    headView = [[UIView alloc]initWithFrame:CGRectMake(0, 20*PERCEN, _scrollView.width, 120*PERCEN)];
    headView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:headView];
    
    headLb = [[UILabel alloc]initWithFrame:CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (headView.height-20)/2., 80, 20)];
    headLb.backgroundColor = [UIColor clearColor];
    headLb.textAlignment = NSTextAlignmentLeft;
    headLb.textColor = UIColorFromHex(0x333333);
    headLb.font = [UIFont systemFontOfSize:14.f];
    headLb.text = @"头像";
    [headView addSubview:headLb];
    
    UIImage *rightImg = [UIImage imageNamed:@"arrow"];
    rightView= [[UIImageView alloc]initWithFrame:CGRectMake(headView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-rightImg.size.width, (headView.height-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height)];
    rightView.backgroundColor = [UIColor clearColor];
    rightView.image = rightImg;
    rightView.tag = 102;
    [headView addSubview:rightView];

    _headImgView = [[UIImageView alloc]initWithFrame:CGRectMake((ISIPAD?headLb.right+30*PERCEN:rightView.left-110*PERCEN), (headView.height-90*PERCEN)/2., 90*PERCEN, 90*PERCEN)];
    _headImgView.backgroundColor = [UIColor clearColor];
    _headImgView.layer.cornerRadius = _headImgView.height/2.;
    _headImgView.layer.masksToBounds = YES;
    if (model && model.FaceImg.length > 0) {
        if ([model.FaceImg hasPrefix:@"/upload/"]) {
            [_headImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",MUSIC_PLAY_Url,model.FaceImg]] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
        } else {
            [_headImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.FaceImg]] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
        }
    }
    [headView addSubview:_headImgView];
    
    headImgBt = [UIButton buttonWithType:UIButtonTypeCustom];
    headImgBt.backgroundColor = [UIColor clearColor];
    headImgBt.frame = headView.bounds;
    [headImgBt addTarget:self action:@selector(onSelectHeadImgAction) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:headImgBt];

    
    //身份
    identityLb = [[UILabel alloc]initWithFrame:CGRectMake(headLb.left, headView.bottom+20*PERCEN, 180, 20)];
    identityLb.backgroundColor = [UIColor clearColor];
    identityLb.textAlignment = NSTextAlignmentLeft;
    identityLb.textColor = UIColorFromHex(0x333333);
    identityLb.font = [UIFont systemFontOfSize:14.f];
    identityLb.text = @"您的身份是";
    [_scrollView addSubview:identityLb];
    
    identityView = [[UIView alloc]initWithFrame:CGRectMake(0, identityLb.bottom+15*PERCEN, _scrollView.width, (ISIPAD?268*PERCEN:200*PERCEN))];
    identityView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:identityView];
    
    if (model && model.UserStatus) {
        int userStatus = [model.UserStatus intValue];
        if (userStatus == 1) {
            _identityType = IdentityType_Mom;
        }
        else if (userStatus == 2) {
            _identityType = IdentityType_Bad;
        }
        else if (userStatus == 3) {
            _identityType = IdentityType_Other;
        }
        else if (userStatus == 4) {
            _identityType = IdentityType_Box;
        }
    }
    float itemWidth = identityView.width / 4.;
    float itemLeft = 0;
    if (ISIPAD) {
        itemWidth = 291*PERCEN;
        itemLeft = (SCREEN_WIDTH-itemWidth*4)/2.;
    }
    for (int i=10; i<14; i++) {
        UIImageView *identityImgView= [[UIImageView alloc]initWithFrame:CGRectMake(itemLeft+35*PERCEN*(ISIPAD?2:1)+itemWidth*(i-10), 20*PERCEN, itemWidth-35*PERCEN*2*(ISIPAD?2:1), itemWidth-35*PERCEN*2*(ISIPAD?2:1))];
        if (!ISIPAD && !ISPortrait) {
            identityImgView.width = (WIDTH_5S ?50:60);
            identityImgView.height = identityImgView.width;
            identityImgView.left = (itemWidth-identityImgView.width)/2.+itemWidth*(i-10);
        }
        identityImgView.backgroundColor = [UIColor clearColor];
        identityImgView.layer.cornerRadius = _headImgView.height/2.;
        identityImgView.layer.masksToBounds = YES;
        identityImgView.tag = 1000+i;
        [identityView addSubview:identityImgView];
        
        UIImage *onSelectImg = [UIImage imageNamed:@"weigouxuan_fb"];
        UIImageView *selectImgView= [[UIImageView alloc]initWithFrame:CGRectMake(itemLeft+itemWidth/2.-10*PERCEN-onSelectImg.size.width+itemWidth*(i-10), identityImgView.bottom+10*PERCEN, onSelectImg.size.width, onSelectImg.size.height)];
        selectImgView.backgroundColor = [UIColor clearColor];
        selectImgView.bottom = identityView.height - 20*PERCEN;
        selectImgView.layer.cornerRadius = selectImgView.height/2.;
        selectImgView.layer.masksToBounds = YES;
        selectImgView.image = onSelectImg;
        selectImgView.tag = 100+i;
        [identityView addSubview:selectImgView];
        
        UILabel *selectNameLb = [[UILabel alloc]initWithFrame:CGRectMake(itemLeft+itemWidth/2.+itemWidth*(i-10), selectImgView.top-2, identityView.width/2., 20)];
        selectNameLb.backgroundColor = [UIColor clearColor];
        selectNameLb.textAlignment = NSTextAlignmentLeft;
        selectNameLb.textColor = UIColorFromHex(0x333333);
        selectNameLb.font = [UIFont systemFontOfSize:13.f];
        selectNameLb.tag = 1004+i;
        [identityView addSubview:selectNameLb];
        
        UIButton *selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
        selectBt.backgroundColor = [UIColor clearColor];
        selectBt.frame = CGRectMake(itemLeft+itemWidth*(i-10), 0, itemWidth, identityView.height);
        [selectBt addTarget:self action:@selector(onSelectIdentityAction:) forControlEvents:UIControlEventTouchUpInside];
        selectBt.tag = i;
        [identityView addSubview:selectBt];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(itemLeft+itemWidth-1+itemWidth*(i-10), 0, 1, identityView.height)];
        lineView.backgroundColor = [UIColorFromHex(0xcccccc) colorWithAlphaComponent:.35];
        lineView.tag = 1008+i;
        [identityView addSubview:lineView];
        
        if (i == 10) {
            identityImgView.image = [UIImage imageNamed:@"mama_zl"];
            selectNameLb.text = @"妈妈";
        }
        else if (i == 11) {
            identityImgView.image = [UIImage imageNamed:@"baba_zl"];
            selectNameLb.text = @"爸爸";
        }
        else if (i == 12) {
            identityImgView.image = [UIImage imageNamed:@"qita_zl"];
            selectNameLb.text = @"其他";
        }
        else if (i == 13) {
            identityImgView.image = [UIImage imageNamed:@"zhubo_zl"];
            selectNameLb.text = @"主播";
            lineView.hidden = YES;
        }
        if (i == _identityType) {
            selectImgView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
    }
    
    //******************************昵称******************************
    _nickView = [[UIView alloc]initWithFrame:CGRectMake(0, identityView.bottom+20*PERCEN, _scrollView.width, 90*PERCEN)];
    _nickView.backgroundColor = [UIColor whiteColor];
    _nickView.hidden = YES;
    [_scrollView addSubview:_nickView];
    
    nickLb = [[UILabel alloc]initWithFrame:CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (90*PERCEN-20)/2., 120*PERCEN, 20)];
    nickLb.backgroundColor = [UIColor clearColor];
    nickLb.textAlignment = NSTextAlignmentLeft;
    nickLb.textColor = UIColorFromHex(0x333333);
    nickLb.font = [UIFont systemFontOfSize:13.f];
    nickLb.text = @"昵称";
    [_nickView addSubview:nickLb];
    
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(nickLb.right, nickLb.top, 180, 20)];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x999999);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [_nickView addSubview:_nickNameLb];
    
    UIImage *nickRightImg = [UIImage imageNamed:@"arrow"];
    nickRightView= [[UIImageView alloc]initWithFrame:CGRectMake(headView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-nickRightImg.size.width, (_nickView.height-rightImg.size.height)/2., nickRightImg.size.width, nickRightImg.size.height)];
    nickRightView.backgroundColor = [UIColor clearColor];
    nickRightView.image = nickRightImg;
    [_nickView addSubview:nickRightView];
    
    nickBt = [UIButton buttonWithType:UIButtonTypeCustom];
    nickBt.backgroundColor = [UIColor clearColor];
    nickBt.frame = _nickView.bounds;
    [nickBt addTarget:self action:@selector(onSelectNickAction) forControlEvents:UIControlEventTouchUpInside];
    [_nickView addSubview:nickBt];
    
    //******************************宝宝信息******************************
    _babyView = [[UIView alloc]initWithFrame:CGRectMake(0, identityView.bottom+20*PERCEN, _scrollView.width, 500*PERCEN)];
    _babyView.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:_babyView];
    
    //名字
    babyNameView = [[UIView alloc]initWithFrame:CGRectMake(0,20*PERCEN, _scrollView.width, 180*PERCEN)];
    babyNameView.backgroundColor = [UIColor whiteColor];
    [_babyView addSubview:babyNameView];
    
    babyNmLb = [[UILabel alloc]initWithFrame:CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (90*PERCEN-20)/2., (WIDTH_5S?140*PERCEN:120*PERCEN), 20)];
    babyNmLb.backgroundColor = [UIColor clearColor];
    babyNmLb.textAlignment = NSTextAlignmentLeft;
    babyNmLb.textColor = UIColorFromHex(0x333333);
    babyNmLb.font = [UIFont systemFontOfSize:13.f];
    babyNmLb.text = @"宝宝名字";
    [babyNameView addSubview:babyNmLb];
    
    _babyNameLb = [[UILabel alloc]initWithFrame:CGRectMake(babyNmLb.right, nickLb.top, 180, 20)];
    _babyNameLb.backgroundColor = [UIColor clearColor];
    _babyNameLb.textAlignment = NSTextAlignmentLeft;
    _babyNameLb.textColor = UIColorFromHex(0x999999);
    _babyNameLb.font = [UIFont systemFontOfSize:13.f];
    [babyNameView addSubview:_babyNameLb];
    
    babyRightView= [[UIImageView alloc]initWithFrame:CGRectMake(babyNameView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-nickRightImg.size.width, (babyNameView.height/2.-rightImg.size.height)/2., nickRightImg.size.width, nickRightImg.size.height)];
    babyRightView.backgroundColor = [UIColor clearColor];
    babyRightView.image = nickRightImg;
    [babyNameView addSubview:babyRightView];
    
    babyNameBt = [UIButton buttonWithType:UIButtonTypeCustom];
    babyNameBt.backgroundColor = [UIColor clearColor];
    babyNameBt.frame = babyNameView.bounds;
    babyNameBt.height = babyNameView.height/2.;
    [babyNameBt addTarget:self action:@selector(onSelectBabyNameAction) forControlEvents:UIControlEventTouchUpInside];
    [babyNameView addSubview:babyNameBt];
    
    line2View = [[UIView alloc]initWithFrame:CGRectMake(0, 89*PERCEN, babyNameView.width, 1)];
    line2View.backgroundColor = [UIColorFromHex(0xcccccc) colorWithAlphaComponent:.35];
    [babyNameView addSubview:line2View];
    
    babySRLb = [[UILabel alloc]initWithFrame:CGRectMake(babyNmLb.left, 90*PERCEN + (90*PERCEN-20)/2., babyNmLb.width, 20)];
    babySRLb.backgroundColor = [UIColor clearColor];
    babySRLb.textAlignment = NSTextAlignmentLeft;
    babySRLb.textColor = UIColorFromHex(0x333333);
    babySRLb.font = [UIFont systemFontOfSize:13.f];
    babySRLb.text = @"宝宝生日";
    [babyNameView addSubview:babySRLb];
    
    _babyBirthDayLb = [[UILabel alloc]initWithFrame:CGRectMake(babySRLb.right, babySRLb.top, 180, 20)];
    _babyBirthDayLb.backgroundColor = [UIColor clearColor];
    _babyBirthDayLb.textAlignment = NSTextAlignmentLeft;
    _babyBirthDayLb.textColor = UIColorFromHex(0x999999);
    _babyBirthDayLb.font = [UIFont systemFontOfSize:13.f];
    [babyNameView addSubview:_babyBirthDayLb];
    
    babyRight2View= [[UIImageView alloc]initWithFrame:CGRectMake(babyNameView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-nickRightImg.size.width,babyNameView.height/2. + (babyNameView.height/2.-rightImg.size.height)/2., nickRightImg.size.width, nickRightImg.size.height)];
    babyRight2View.backgroundColor = [UIColor clearColor];
    babyRight2View.image = nickRightImg;
    [babyNameView addSubview:babyRight2View];
    
    babyBirthDayBt = [UIButton buttonWithType:UIButtonTypeCustom];
    babyBirthDayBt.backgroundColor = [UIColor clearColor];
    babyBirthDayBt.frame = babyNameView.bounds;
    babyBirthDayBt.height = babyNameView.height/2.;
    babyBirthDayBt.top = babyNameView.height/2.;
    [babyBirthDayBt addTarget:self action:@selector(onSelectBabyBirthdayAction) forControlEvents:UIControlEventTouchUpInside];
    [babyNameView addSubview:babyBirthDayBt];
    
    //性别
    babySexLb = [[UILabel alloc]initWithFrame:CGRectMake(headLb.left, babyNameView.bottom+20*PERCEN, 180, 20)];
    babySexLb.backgroundColor = [UIColor clearColor];
    babySexLb.textAlignment = NSTextAlignmentLeft;
    babySexLb.textColor = UIColorFromHex(0x333333);
    babySexLb.font = [UIFont systemFontOfSize:14.f];
    babySexLb.text = @"宝宝的性别";
    [_babyView addSubview:babySexLb];
    if (_identityType == IdentityType_Mom ||_identityType == IdentityType_Bad) {
        _babyView.hidden = NO;
        _nickView.hidden = YES;
        if (model && model.NickName.length > 0) {
            _babyNameLb.text = model.NickName;
        }
        if (model && model.BabyBirthday.length > 0) {
            _babyBirthDayLb.text = model.BabyBirthday;
        }
    }
    else if (_identityType == IdentityType_Other ||_identityType == IdentityType_Box) {
        _nickView.hidden = NO;
        _babyView.hidden = YES;
        if (model && model.NickName.length > 0) {
            _nickNameLb.text = model.NickName;
        }
    }
    
    
    babySexView = [[UIView alloc]initWithFrame:CGRectMake(0, babySexLb.bottom+15*PERCEN, _scrollView.width, (ISIPAD?282*PERCEN:200*PERCEN))];
    babySexView.backgroundColor = [UIColor whiteColor];
    [_babyView addSubview:babySexView];
    
    if (model && model.BabySex > 0) {
        int sexBaby = [model.BabySex intValue];
        if (sexBaby == 1) {
            _babySexType = BabySexType_Boy;
        }
        else if (sexBaby == 2) {
            _babySexType = BabySexType_Girl;
        }
        else {
            _babySexType = BabySexType_Seciry;
        }
    }
    
    itemWidth = _babyView.width / 2.;
    float picItem = ISIPAD?(180*PERCEN): (WIDTH_5S ?50:60);
    for (int i=30; i<32; i++) {
        UIImageView *identityImgView= [[UIImageView alloc]initWithFrame:CGRectMake((itemWidth-picItem)/2.+itemWidth*(i-30), 20*PERCEN, picItem, picItem)];
        identityImgView.backgroundColor = [UIColor clearColor];
        identityImgView.layer.cornerRadius = identityImgView.height/2.;
        identityImgView.layer.masksToBounds = YES;
        identityImgView.tag = 22+i;
        [babySexView addSubview:identityImgView];
        
        UIImage *onSelectImg = [UIImage imageNamed:@"weigouxuan_fb"];
        UIImageView *selectImgView= [[UIImageView alloc]initWithFrame:CGRectMake(itemWidth/2.-10*PERCEN-onSelectImg.size.width+itemWidth*(i-30), identityImgView.bottom+10*PERCEN, onSelectImg.size.width, onSelectImg.size.height)];
        selectImgView.backgroundColor = [UIColor clearColor];
        selectImgView.bottom = identityView.height - 20*PERCEN;
        selectImgView.layer.cornerRadius = selectImgView.height/2.;
        selectImgView.layer.masksToBounds = YES;
        selectImgView.image = onSelectImg;
        selectImgView.tag = 200+i;
        [babySexView addSubview:selectImgView];
        
        UILabel *selectNameLb = [[UILabel alloc]initWithFrame:CGRectMake(itemWidth/2.+itemWidth*(i-30), selectImgView.top-2, identityView.width/2., 20)];
        selectNameLb.backgroundColor = [UIColor clearColor];
        selectNameLb.textAlignment = NSTextAlignmentLeft;
        selectNameLb.textColor = UIColorFromHex(0x333333);
        selectNameLb.font = [UIFont systemFontOfSize:13.f];
        selectNameLb.tag = 24+i;
        [babySexView addSubview:selectNameLb];
        
        UIButton *selectBt = [UIButton buttonWithType:UIButtonTypeCustom];
        selectBt.backgroundColor = [UIColor clearColor];
        selectBt.frame = CGRectMake(itemWidth*(i-30), 0, itemWidth, identityView.height);
        [selectBt addTarget:self action:@selector(onSelectBabySexAction:) forControlEvents:UIControlEventTouchUpInside];
        selectBt.tag = i;
        [babySexView addSubview:selectBt];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(itemWidth-1+itemWidth*(i-30), 0, 1, identityView.height)];
        lineView.backgroundColor = [UIColorFromHex(0xcccccc) colorWithAlphaComponent:.35];
        lineView.tag = 26+i;
        [babySexView addSubview:lineView];
        
        if (i == 30) {
            identityImgView.image = [UIImage imageNamed:@"nanbao_zl"];
            selectNameLb.text = @"男宝宝";
        }
        else if (i == 31) {
            identityImgView.image = [UIImage imageNamed:@"nvbao_zl"];
            selectNameLb.text = @"女宝宝";
            lineView.hidden = YES;
        }
        if (i == _babySexType) {
            selectImgView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
    }
    
    //时间选择器
    _myDatePicker = [[UIDatePicker alloc] init];
    _myDatePicker.frame = CGRectMake(0, _scrollView.height+50*PERCEN, SCREEN_WIDTH, 216);
    _myDatePicker.hidden = YES;
    _myDatePicker.backgroundColor = [UIColor whiteColor];
    NSTimeInterval secondPerDay = 24 * 60 * 60;
    NSTimeInterval secondPer2Year = secondPerDay * 356 * 20;
    NSDate *today = [[NSDate alloc] init];
    NSDate *min = [today dateByAddingTimeInterval: -secondPer2Year];
    NSDate *max = [today dateByAddingTimeInterval: secondPer2Year];
    //根据手机本地设置显示中文还是其他语言
    [_myDatePicker setLocale: [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    //设置DatePicker的时区。
    [_myDatePicker setTimeZone:[NSTimeZone defaultTimeZone]];
    [_myDatePicker setDatePickerMode: UIDatePickerModeDate]; // 设置日期选择器模式
    [_myDatePicker setDate: today animated: YES]; // 设置默认选中日期
    [_myDatePicker setMinimumDate: min]; // 设置允许选择的最小日期
    [_myDatePicker setMaximumDate: max]; // 设置允许选择的最大日期
//    [[UIApplication sharedApplication].keyWindow addSubview: _myDatePicker];
    [_scrollView addSubview:_myDatePicker];
    
    _datePickerView = [[UIView alloc]initWithFrame:CGRectMake(0, _myDatePicker.top-50*PERCEN, SCREEN_WIDTH, 50*PERCEN)];
    _datePickerView.backgroundColor = UIColorFromHex(0xf5f5f5);
    _datePickerView.hidden = YES;
//    [[UIApplication sharedApplication].keyWindow addSubview:_datePickerView];
    [_scrollView addSubview:_datePickerView];
    
    cancelBt = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBt.backgroundColor = [UIColor clearColor];
    cancelBt.frame = CGRectMake(40*PERCEN, 0, 120, _datePickerView.height);
    [cancelBt setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancelBt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [cancelBt setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 80)];
    [cancelBt addTarget:self action:@selector(onSelectCancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_datePickerView addSubview:cancelBt];
    
    sureBt = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBt.backgroundColor = [UIColor clearColor];
    sureBt.frame = CGRectMake(_datePickerView.width - 40*PERCEN - 120, 0, 120, _datePickerView.height);
    [sureBt setTitle:@"确认" forState:UIControlStateNormal];
    [sureBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sureBt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [sureBt setTitleEdgeInsets:UIEdgeInsetsMake(0, 80, 0, 0)];
    [sureBt addTarget:self action:@selector(onSelectSureAction) forControlEvents:UIControlEventTouchUpInside];
    [_datePickerView addSubview:sureBt];
    
    _showderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 216 - 50*PERCEN)];
    _showderView.backgroundColor = [UIColorFromHex(0x000000) colorWithAlphaComponent:.2];
    _showderView.hidden = YES;
    _showderView.userInteractionEnabled = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:_showderView];
    
    _scrollView.contentSize = CGSizeMake(_scrollView.width, (_scrollView.height >= _babyView.bottom ? _scrollView.height-NAV_HEIGHT : _babyView.bottom));
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    headView.frame = CGRectMake(0, 20*PERCEN, _scrollView.width, 120*PERCEN);
    headLb.frame = CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (headView.height-20)/2., 80, 20);
    UIImage *rightImg = [UIImage imageNamed:@"arrow"];
    rightView.frame = CGRectMake(headView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-rightImg.size.width, (headView.height-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height);
    _headImgView.frame = CGRectMake((ISIPAD?headLb.right+30*PERCEN:rightView.left-110*PERCEN), (headView.height-90*PERCEN)/2., 90*PERCEN, 90*PERCEN);
    headImgBt.frame = headView.bounds;
    //********
    identityLb.frame = CGRectMake(headLb.left, headView.bottom+20*PERCEN, 180, 20);
    identityView.frame = CGRectMake(0, identityLb.bottom+15*PERCEN, _scrollView.width, (ISIPAD?268*PERCEN:200*PERCEN));
    float itemWidth = identityView.width / 4.;
    float itemLeft = 0;
    if (ISIPAD) {
        itemWidth = 291*PERCEN;
        itemLeft = (SCREEN_WIDTH-itemWidth*4)/2.;
    }
    for (int i=10; i<14; i++) {
        UIImageView *identityImgView= (UIImageView *)[self.view viewWithTag:1000+i];
        UIImageView *selectImgView=(UIImageView *)[self.view viewWithTag:100+i];
        UILabel *selectNameLb = (UILabel *)[self.view viewWithTag:1004+i];
        UIButton *selectBt = (UIButton *)[self.view viewWithTag:i];
        UIView *lineView = (UIView *)[self.view viewWithTag:i+1008];
        
        identityImgView.frame = CGRectMake(itemLeft+35*PERCEN*(ISIPAD?2:1)+itemWidth*(i-10), 20*PERCEN, itemWidth-35*PERCEN*2*(ISIPAD?2:1), itemWidth-35*PERCEN*2*(ISIPAD?2:1));
        if (!ISIPAD && !ISPortrait) {
            identityImgView.width = (WIDTH_5S ?50:60);
            identityImgView.height = identityImgView.width;
            identityImgView.left = (itemWidth-identityImgView.width)/2.+itemWidth*(i-10);
        }
        UIImage *onSelectImg = [UIImage imageNamed:@"weigouxuan_fb"];
        selectImgView.frame = CGRectMake(itemLeft+itemWidth/2.-10*PERCEN-onSelectImg.size.width+itemWidth*(i-10), identityImgView.bottom+10*PERCEN, onSelectImg.size.width, onSelectImg.size.height);
        selectImgView.bottom = identityView.height - 20*PERCEN;
        selectNameLb.frame = CGRectMake(itemLeft+itemWidth/2.+itemWidth*(i-10), selectImgView.top-2, identityView.width/2., 20);
        selectBt.frame = CGRectMake(itemLeft+itemWidth*(i-10), 0, itemWidth, identityView.height);
        lineView.frame = CGRectMake(itemLeft+itemWidth-1+itemWidth*(i-10), 0, 1, identityView.height);
    }
    
    //*****************昵称**********
    _nickView.frame = CGRectMake(0, identityView.bottom+20*PERCEN, _scrollView.width, 90*PERCEN);
    nickLb.frame = CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (90*PERCEN-20)/2., 120*PERCEN, 20);
    _nickNameLb.frame = CGRectMake(nickLb.right, nickLb.top, 180, 20);
    UIImage *nickRightImg = [UIImage imageNamed:@"arrow"];
    nickRightView.frame = CGRectMake(headView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-nickRightImg.size.width, (_nickView.height-rightImg.size.height)/2., nickRightImg.size.width, nickRightImg.size.height);
    nickBt.frame = _nickView.bounds;
    
    //******************************宝宝信息******************************
    _babyView.frame = CGRectMake(0, identityView.bottom+20*PERCEN, _scrollView.width, 500*PERCEN);
    babyNameView.frame = CGRectMake(0,20*PERCEN, _scrollView.width, 180*PERCEN);
    babyNmLb.frame = CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (90*PERCEN-20)/2., (WIDTH_5S?140*PERCEN:120*PERCEN), 20);
    _babyNameLb.frame = CGRectMake(babyNmLb.right, nickLb.top, 180, 20);
    babyRightView.frame = CGRectMake(babyNameView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-nickRightImg.size.width, (babyNameView.height/2.-rightImg.size.height)/2., nickRightImg.size.width, nickRightImg.size.height);
    babyNameBt.frame = babyNameView.bounds;
    babyNameBt.height = babyNameView.height/2.;
    
    line2View.frame = CGRectMake(0, 89*PERCEN, babyNameView.width, 1);
    babySRLb.frame = CGRectMake(babyNmLb.left,90*PERCEN + (90*PERCEN-20)/2., babyNmLb.width, 20);
    _babyBirthDayLb.frame = CGRectMake(babySRLb.right, babySRLb.top, 180, 20);
    babyRight2View.frame = CGRectMake(babyNameView.width-(ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-nickRightImg.size.width,babyNameView.height/2. + (babyNameView.height/2.-rightImg.size.height)/2., nickRightImg.size.width, nickRightImg.size.height);
    babyBirthDayBt.frame = babyNameView.bounds;
    babyBirthDayBt.height = babyNameView.height/2.;
    babyBirthDayBt.top = babyNameView.height/2.;
    
    //性别
    babySexLb.frame = CGRectMake(headLb.left, babyNameView.bottom+20*PERCEN, 180, 20);
    babySexView.frame = CGRectMake(0, babySexLb.bottom+15*PERCEN, _scrollView.width, (ISIPAD?282*PERCEN:200*PERCEN));
    itemWidth = _babyView.width / 2.;
    float picItem = ISIPAD?(180*PERCEN): (WIDTH_5S ?50:60);
    for (int i=30; i<32; i++) {
        UIImageView *identityImgView= (UIImageView *)[self.view viewWithTag:22+i];
        UIImageView *selectImgView= (UIImageView *)[self.view viewWithTag:200+i];
        UILabel *selectNameLb = (UILabel *)[self.view viewWithTag:24+i];
        UIButton *selectBt = (UIButton *)[self.view viewWithTag:i];
        UIView *lineView = (UIView *)[self.view viewWithTag:i+26];
        
        identityImgView.frame = CGRectMake((itemWidth-picItem)/2.+itemWidth*(i-30), 20*PERCEN, picItem, picItem);
        UIImage *onSelectImg = [UIImage imageNamed:@"weigouxuan_fb"];
        selectImgView.frame = CGRectMake(itemWidth/2.-10*PERCEN-onSelectImg.size.width+itemWidth*(i-30), identityImgView.bottom+10*PERCEN, onSelectImg.size.width, onSelectImg.size.height);
        selectImgView.bottom = identityView.height - 20*PERCEN;
        selectNameLb.frame = CGRectMake(itemWidth/2.+itemWidth*(i-30), selectImgView.top-2, identityView.width/2., 20);
        selectBt.frame = CGRectMake(itemWidth*(i-30), 0, itemWidth, identityView.height);
        lineView.frame = CGRectMake(itemWidth-1+itemWidth*(i-30), 0, 1, identityView.height);
    }
    
    //时间选择器
    if (ISIPAD) {
        _myDatePicker.frame = CGRectMake(0, (_myDatePicker.isHidden?SCREEN_HEIGHT+50*PERCEN:SCREEN_HEIGHT-NAV_HEIGHT - 216), SCREEN_WIDTH, 216);
        _datePickerView.frame = CGRectMake(0, _myDatePicker.top-50*PERCEN, SCREEN_WIDTH, 50*PERCEN);
        cancelBt.frame = CGRectMake(40*PERCEN, 0, 120, _datePickerView.height);
        sureBt.frame = CGRectMake(_datePickerView.width - 40*PERCEN - 120, 0, 120, _datePickerView.height);
        _showderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 216 - 50*PERCEN);
        _scrollView.contentSize = CGSizeMake(_scrollView.width, (_scrollView.height >= _babyView.bottom ? _scrollView.height-NAV_HEIGHT : _babyView.bottom));
    } else {
        _myDatePicker.frame = CGRectMake(0, (_myDatePicker.isHidden?SCREEN_HEIGHT+50*PERCEN:SCREEN_HEIGHT - 216), SCREEN_WIDTH, 216);
        _datePickerView.frame = CGRectMake(0, _myDatePicker.top-50*PERCEN, SCREEN_WIDTH, 50*PERCEN);
        cancelBt.frame = CGRectMake(40*PERCEN, 0, 120, _datePickerView.height);
        sureBt.frame = CGRectMake(_datePickerView.width - 40*PERCEN - 120, 0, 120, _datePickerView.height);
        _showderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT +(ISPortrait?NAV_HEIGHT:34)- 216-50*PERCEN);
        _scrollView.contentSize = CGSizeMake(_scrollView.width, (_scrollView.height >= _babyView.bottom ? _scrollView.height-(ISPortrait?NAV_HEIGHT:30) : _babyView.bottom));
    }

}

#pragma mark - 数据加载
- (void)onLoadData
{
    UserModel *model = [UserManager standerDefult].userModel;
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_GETINFO_URL_PATH,model.UserID] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json[@"ret_code"] intValue] == 0) {
            NSArray *arr = json[@"data"];
            if ([arr isKindOfClass:[NSArray class]]) {
                
            }
        } else {
            [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark - Actions
//完成
- (void)onSelectItemForWork
{
    if (![UserManager standerDefult].userModel.UserID) {
        return;
    }
    if (_headImgView.image) {
        //    NSString *url = [[NSBundle mainBundle]pathForResource:@"情非得已" ofType:@"mp3"];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"完善资料...";
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                             @"text/html",
                                                             @"image/jpeg",
                                                             @"image/png",
                                                             @"application/octet-stream",
                                                             @"text/json",
                                                             nil];
        [manager POST:[NSString stringWithFormat:UPLOAD_FILE_URL_PATH,[UserManager standerDefult].userModel.UserID] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            NSData *data = UIImagePNGRepresentation(_headImgView.image);
            //        NSData *data = [NSData dataWithContentsOfFile:url];
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
            NSString *dateString = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"HeaderImg_%@.png",dateString];
            [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"multipart/form-data"];//@"multipart/form-data"image/jpg/file
            //        [formData appendPartWithFileData:data name:@"file" fileName:@"loadPic.mp3" mimeType:@"multipart/form-data"];
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                if ([[responseObject objectForKey:@"ret_code"]intValue] == 0) {
                    NSString *webUrl = [responseObject objectForKey:@"web_url"];
                    [self loadDataWithUrl:webUrl];
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [self loadDataWithUrl:nil];
        }];
    } else {
        [self loadDataWithUrl:nil];
    }
}

- (void)loadDataWithUrl:(NSString *)webUrl
{
    //用户ID,用户昵称,用户头像图片路径,用户身份,用户签名,宝宝生日,宝宝性别
    NSNumber *UserStatus = [NSNumber numberWithInt:(_identityType-9)];
    NSString *name = (_babyNameLb.text.length > 0 ? _babyNameLb.text : (_nickNameLb.text.length > 0 ? _nickNameLb.text:@""));
    NSString *BabyBirthday = (_babyBirthDayLb.text.length > 0 ?_babyBirthDayLb.text:@"");
    NSNumber *BabySex = [NSNumber numberWithInt:3];
    NSString *Signature = ([UserManager standerDefult].userModel.Signature>0 ? [UserManager standerDefult].userModel.Signature:@"宝宝爱吃爱喝爱睡觉，不服你来比一比!哼哼。");
    if ([UserStatus intValue] > 2) {
        //其他和主播
        BabySex = [NSNumber numberWithInt:3];
    } else {
        BabySex = [NSNumber numberWithInt:(_babySexType-29)];
        if ([BabySex intValue] < 0 || [BabySex intValue] > 3) {
            BabySex = [NSNumber numberWithInt:3];
        }
    }
    
    [[HTTPManager standerDefault]AFPostRequest:[NSString stringWithFormat:UPDATE_USER_INFO_URL_PATH,[UserManager standerDefult].userModel.UserID,name,(webUrl.length > 0 ? webUrl:@""),UserStatus,Signature,BabyBirthday,BabySex] view:nil parameters:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json[@"ret_code"] integerValue]==0) {
            
            UserModel *model = [UserManager standerDefult].userModel;
            if (webUrl.length > 0) {
                model.FaceImg = webUrl;
            }
            if (name.length > 0) {
                model.NickName = name;
            }
            if ([UserStatus intValue] >=1 && [UserStatus intValue]<=4) {
                model.UserStatus = UserStatus;
            }
            if (Signature.length > 0) {
                model.Signature = Signature;
            }
            if (BabyBirthday.length > 0) {
                model.BabyBirthday = BabyBirthday;
            }
            if ([BabySex intValue] >= 0 && [BabySex intValue] <= 3) {
                model.BabySex = [NSString stringWithFormat:@"%@",BabySex];
            }
            
            if (_isFromRegister) {
                if (_formBaseViewCtrl) {
                    for (UIViewController *controller in self.navigationController.viewControllers) {
                        if ([_formBaseViewCtrl isEqualToString:NSStringFromClass([controller class])]) {
                            [self.navigationController popToViewController:controller animated:YES];
                        }
                    }
                } else {                    
                    MBTabBarController *tabBar = [[MBTabBarController alloc] init];
                    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    delegate.window.rootViewController = tabBar;
                }
            } else {
                [self back];
            }

        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)onSelectIdentityAction:(UIButton *)bt
{
    if (bt.tag == _identityType) {
        return;
    }
    
    UIImageView *imgSelectView = (UIImageView *)[self.view viewWithTag:100+bt.tag];
    UIImageView *imgView = (UIImageView *)[self.view viewWithTag:100+_identityType];
    imgView.image = [UIImage imageNamed:@"weigouxuan_fb"];
    switch (bt.tag) {
        case 10:
        {
            _nickView.hidden = YES;
            _babyView.hidden = !_nickView.hidden;
            imgSelectView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
            break;
        case 11:
        {
             _nickView.hidden = YES;
            _babyView.hidden = !_nickView.hidden;
            imgSelectView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
            break;
        case 12:
        {
             _nickView.hidden = NO;
            _babyView.hidden = !_nickView.hidden;
            imgSelectView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
            break;
        case 13:
        {
            _nickView.hidden = NO;
            _babyView.hidden = !_nickView.hidden;
            imgSelectView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
            break;
            
        default:
            break;
    }
    _identityType = (int)bt.tag;
}

- (void)onSelectBabySexAction:(UIButton *)bt
{
    if (bt.tag == _babySexType) {
        return;
    }
    UIImageView *imgSelectView = (UIImageView *)[self.view viewWithTag:200+bt.tag];
    UIImageView *imgView = (UIImageView *)[self.view viewWithTag:200+_babySexType];
    imgView.image = [UIImage imageNamed:@"weigouxuan_fb"];
    switch (bt.tag) {
        case 30:
        {
            imgSelectView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
            break;
        case 31:
        {
            imgSelectView.image = [UIImage imageNamed:@"gouxuan_fb"];
        }
            break;
            
        default:
            break;
    }
    _babySexType = (int)bt.tag;
}

//昵称设置
- (void)onSelectNickAction
{
    MBSpaceInFormationViewController *information = [[MBSpaceInFormationViewController alloc]init];
    information.isSign = NO;
    information.signTitle = _nickNameLb.text;
    information.InformationBlock = ^(NSString *showName) {
        _nickNameLb.text = showName;
    };
    [self.navigationController pushViewController:information animated:YES];
}

//宝宝名字设置
- (void)onSelectBabyNameAction
{
    MBSpaceInFormationViewController *information = [[MBSpaceInFormationViewController alloc]init];
    information.isSign = NO;
    information.signTitle = _babyNameLb.text;
    information.InformationBlock = ^(NSString *showName) {
        _babyNameLb.text = showName;
    };
    [self.navigationController pushViewController:information animated:YES];
}

//日历
#pragma mark - 日历
- (void)onSelectBabyBirthdayAction
{
    if (_myDatePicker.top >= _scrollView.height+50*PERCEN) {
        [UIView animateWithDuration:.3 animations:^{
            _myDatePicker.top = (ISIPAD?SCREEN_HEIGHT-NAV_HEIGHT - 216:SCREEN_HEIGHT - 216);
            _datePickerView.top = (ISIPAD?_myDatePicker.top-_datePickerView.height:_myDatePicker.top-50*PERCEN);
            _showderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, (ISIPAD?SCREEN_HEIGHT - 216 - 50*PERCEN:SCREEN_HEIGHT +(ISPortrait?NAV_HEIGHT:34)- 216-50*PERCEN));
            _showderView.hidden = NO;
            _myDatePicker.hidden = _showderView.isHidden;
            _datePickerView.hidden = _showderView.isHidden;
        }];
    } else {
        [UIView animateWithDuration:.3 animations:^{
            _myDatePicker.top = SCREEN_HEIGHT+50*PERCEN;
            _datePickerView.top = _myDatePicker.top - _datePickerView.height;
            _showderView.hidden = YES;
            _myDatePicker.hidden = _showderView.isHidden;
            _datePickerView.hidden = _showderView.isHidden;
        }];
    }
}

- (void)onSelectCancelAction
{
    [UIView animateWithDuration:.3 animations:^{
        _myDatePicker.top = SCREEN_HEIGHT+50*PERCEN;
        _datePickerView.top = _myDatePicker.top - _datePickerView.height;
        _showderView.hidden = YES;
        _myDatePicker.hidden = _showderView.isHidden;
        _datePickerView.hidden = _showderView.isHidden;
    }];
}

- (void)onSelectSureAction
{
    NSDate *date = _myDatePicker.date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    _babyBirthDayLb.text = [formatter stringFromDate:date];
    
    [self onSelectCancelAction];
}

//弹出相片选择框
- (void)onSelectHeadImgAction
{
    if (IOS(8.0)) {
        UIAlertController *alertContr = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        __weak typeof(self) weakSelf = self;
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"打开照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf onChooseType:UIImagePickerControllerSourceTypeCamera];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf onChooseType:UIImagePickerControllerSourceTypePhotoLibrary];
        }];
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertContr addAction:action1];
        [alertContr addAction:action2];
        [alertContr addAction:action3];
        [action1 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
        [action2 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
        [action3 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
        if (ISIPAD) {
            UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"打开照相机" otherButtonTitles:@"从手机相册获取", nil];
            [sheet showInView:self.view];
        } else {
            [self.navigationController presentViewController:alertContr animated:YES completion:NULL];
        }
    } else {
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"打开照相机" otherButtonTitles:@"从手机相册获取", nil];
        [sheet showInView:self.view];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self onChooseType:UIImagePickerControllerSourceTypeCamera];
    }
    else if (buttonIndex == 1) {
        [self onChooseType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    else if (buttonIndex == 2) {
        
    }
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *view in actionSheet.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)view;
            label.textColor = [UIColor blackColor];
        }
    }
}

#pragma mark - 相片
- (void)onChooseType:(UIImagePickerControllerSourceType)sourceType
{
    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
        if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
            NSLog(@"该手机不能拍照");
            return;
        }
    }
    
    UIImagePickerController *pickerCtrl = [[UIImagePickerController alloc]init];
    pickerCtrl.sourceType = sourceType;
    pickerCtrl.delegate = self;
    pickerCtrl.allowsEditing = YES;
    if (ISIPAD) {
        pickerCtrl.modalPresentationStyle = UIModalPresentationFormSheet;
    }
    if (ISIPAD) {
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            UIPopoverController *poc = [[UIPopoverController alloc]initWithContentViewController:pickerCtrl];
            poc.popoverContentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
            [poc presentPopoverFromRect:CGRectMake(0, 0, SCREEN_WIDTH, identityLb.top) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        }];
    } else {
        [self.navigationController presentViewController:pickerCtrl animated:YES completion:NULL];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    if (info.count > 0) {
        UIImage *originImg = [info objectForKey:UIImagePickerControllerEditedImage];
        _headImgView.image = [UIImage imageWithData:[self onGetPictureForIMG:originImg]];
        
         [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    }
}

- (NSData *)onGetPictureForIMG:(UIImage *)img
{
    CGRect rect = CGRectMake(0, 0, 300, 300);
    UIGraphicsBeginImageContext(rect.size);
    [img drawInRect:rect];
    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImagePNGRepresentation(newImg);
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
}

@end
